package com.liull.papersystem.controller;

import com.liull.papersystem.common.Result;
import com.liull.papersystem.entity.UserDo;
import com.liull.papersystem.service.UserService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * @Descriotion:
 * @Author longlong.liu
 * @Version 1.0
 * @Date 2021/10/26 21:14
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public Result login(UserDo userDo, HttpSession session, @RequestParam String captcha, HttpServletRequest request) {
        if (!CaptchaUtil.ver(captcha, request)) {
            return Result.fail("验证码错误");
        }
        UserDo user = userService.login(userDo.getUsername(), userDo.getPassword());
        if (Objects.nonNull(user)) {
            user.setPassword(null);
            session.setAttribute("userInfo", user);
            return Result.success();
        } else
            return Result.fail("用户名或者密码错误");
    }

}
